home *** CD-ROM | disk | FTP | other *** search
/ Aminet 24 / Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso / Aminet / dev / c / cxref_1_4a.lha / amiga.c < prev    next >
C/C++ Source or Header  |  1997-12-07  |  7KB  |  408 lines

  1. /*
  2.  * Amiga specific support code for cxref
  3.  *
  4.  * Written by Olaf Barthel <olsen@sourcery.han.de>
  5.  *     Public Domain
  6.  *
  7.  * :ts=4
  8.  */
  9.  
  10. /******************************************************************************/
  11.  
  12. #include <dos/dosextens.h>
  13. #include <dos/dostags.h>
  14. #include <dos/dosasl.h>
  15.  
  16. #include <exec/memory.h>
  17.  
  18. #include <clib/exec_protos.h>
  19. #include <clib/dos_protos.h>
  20.  
  21. #include <pragmas/exec_sysbase_pragmas.h>
  22. #include <pragmas/dos_pragmas.h>
  23.  
  24. extern struct Library *    SysBase;
  25. extern struct Library *    DOSBase;
  26.  
  27. /******************************************************************************/
  28.  
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <stdio.h>
  32. #include <stat.h>
  33. #include <dos.h>
  34.  
  35. /******************************************************************************/
  36.  
  37. #define MAX_FILENAME_LEN 1024
  38.  
  39. /******************************************************************************/
  40.  
  41. typedef struct NameNode
  42. {
  43.     struct NameNode    *    Next;
  44.     char *                Name;
  45.     BOOL                Wild;
  46. } NameNode;
  47.  
  48. /******************************************************************************/
  49.  
  50. static int
  51. compare(char **a,char **b)
  52. {
  53.     return(stricmp(*a,*b));
  54. }
  55.  
  56. /******************************************************************************/
  57.  
  58. int
  59. expand_args(int argc,char **argv,int *_argc,char ***_argv,int all,int sort)
  60. {
  61.     struct AnchorPath *Anchor;
  62.     LONG Error;
  63.  
  64.     *_argc = argc;
  65.     *_argv = argv;
  66.  
  67.     if(DOSBase->lib_Version < 37)
  68.         return(0);
  69.  
  70.     if(Anchor = (struct AnchorPath *)AllocVec(sizeof(struct AnchorPath) + MAX_FILENAME_LEN,MEMF_ANY | MEMF_CLEAR))
  71.     {
  72.         NameNode *    Root;
  73.         LONG        NamePlus;
  74.         LONG        NameTotal;
  75.         LONG        i;
  76.  
  77.         Root        = NULL;
  78.         NamePlus    = 0;
  79.         NameTotal    = 0;
  80.         Error        = 0;
  81.  
  82.         Anchor->ap_Strlen        = MAX_FILENAME_LEN;
  83.         Anchor->ap_BreakBits    = SIGBREAKF_CTRL_C;
  84.  
  85.         for(i = 0 ; !Error && i < argc ; i++)
  86.         {
  87.             if(i && ParsePatternNoCase(argv[i],Anchor->ap_Buf,MAX_FILENAME_LEN) == 1)
  88.             {
  89.                 NameNode *    Node;
  90.                 LONG        Result;
  91.  
  92.                 Result = MatchFirst(argv[i],Anchor);
  93.  
  94.                 while(!Result)
  95.                 {
  96.                     if(Anchor->ap_Info.fib_DirEntryType < 0)
  97.                     {
  98.                         if(Node = (NameNode *)malloc(sizeof(NameNode) + strlen(Anchor->ap_Buf) + 1))
  99.                         {
  100.                             Node->Name = (char *)(Node + 1);
  101.                             Node->Next = Root;
  102.                             Node->Wild = TRUE;
  103.  
  104.                             strcpy(Node->Name,Anchor->ap_Buf);
  105.  
  106.                             Root = Node;
  107.  
  108.                             NamePlus++;
  109.                             NameTotal++;
  110.                         }
  111.                         else
  112.                         {
  113.                             Result = ERROR_NO_FREE_STORE;
  114.                             break;
  115.                         }
  116.                     }
  117.  
  118.                     if(all && Anchor->ap_Info.fib_DirEntryType > 0)
  119.                     {
  120.                         if(Anchor->ap_Flags & APF_DIDDIR)
  121.                             Anchor->ap_Flags &= ~APF_DIDDIR;
  122.                         else
  123.                             Anchor->ap_Flags |= APF_DODIR;
  124.                     }
  125.  
  126.                     Result = MatchNext(Anchor);
  127.                 }
  128.  
  129.                 if(Result != ERROR_NO_MORE_ENTRIES)
  130.                     Error = Result;
  131.             }
  132.             else
  133.             {
  134.                 NameNode *Node;
  135.  
  136.                 if(Node = (NameNode *)malloc(sizeof(NameNode)))
  137.                 {
  138.                     Node->Name = argv[i];
  139.                     Node->Next = Root;
  140.                     Node->Wild = FALSE;
  141.  
  142.                     Root = Node;
  143.  
  144.                     NameTotal++;
  145.                 }
  146.                 else
  147.                 {
  148.                     Error = ERROR_NO_FREE_STORE;
  149.                 }
  150.             }
  151.         }
  152.  
  153.         if(!Error && NamePlus)
  154.         {
  155.             char **Index;
  156.  
  157.             if(Index = (char **)malloc(sizeof(char *) * (NameTotal + 1)))
  158.             {
  159.                 NameNode *    Node;
  160.                 char **        LastWild;
  161.  
  162.                 *_argc = NameTotal;
  163.                 *_argv = Index;
  164.  
  165.                 Index = &(Index[NameTotal]);
  166.  
  167.                 *Index-- = NULL;
  168.  
  169.                 Node        = Root;
  170.                 LastWild    = NULL;
  171.  
  172.                 while(Node)
  173.                 {
  174.                     if(sort)
  175.                     {
  176.                         if(Node->Wild)
  177.                         {
  178.                             if(!LastWild)
  179.                                 LastWild = Index;
  180.                         }
  181.                         else
  182.                         {
  183.                             if(LastWild)
  184.                             {
  185.                                 if((ULONG)LastWild - (ULONG)Index > sizeof(char **))
  186.                                     qsort(Index + 1,((ULONG)LastWild - (ULONG)Index) / sizeof(char **),sizeof(char *),compare);
  187.  
  188.                                 LastWild = NULL;
  189.                             }
  190.                         }
  191.                     }
  192.  
  193.                     *Index-- = Node->Name;
  194.  
  195.                     Node = Node->Next;
  196.                 }
  197.             }
  198.             else
  199.             {
  200.                 Error = ERROR_NO_FREE_STORE;
  201.             }
  202.         }
  203.  
  204.         if(Error || !NamePlus)
  205.         {
  206.             NameNode *Node,*Next;
  207.  
  208.             Node = Root;
  209.  
  210.             while(Node)
  211.             {
  212.                 Next = Node->Next;
  213.  
  214.                 free(Node);
  215.  
  216.                 Node = Next;
  217.             }
  218.         }
  219.  
  220.         FreeVec(Anchor);
  221.     }
  222.     else
  223.     {
  224.         Error = ERROR_NO_FREE_STORE;
  225.     }
  226.  
  227.     if(Error)
  228.     {
  229.         PrintFault(Error,FilePart(argv[0]));
  230.  
  231.         return(-1);
  232.     }
  233.     else
  234.     {
  235.         return(0);
  236.     }
  237. }
  238.  
  239. /******************************************************************************/
  240.  
  241. FILE *
  242. popen_execvp(char** command)
  243. {
  244.     char tempName[40];
  245.     struct DateStamp stamp;
  246.     ULONG secs;
  247.  
  248.     FILE * result = NULL;
  249.     char *cmd;
  250.     int len,i;
  251.  
  252.     DateStamp(&stamp);
  253.     secs = (stamp.ds_Days * 24 * 60 + stamp.ds_Minute) * 60 + (stamp.ds_Tick / TICKS_PER_SECOND);
  254.  
  255.     sprintf(tempName,"pipe:%08lx.%08lx",FindTask(NULL),secs);
  256.  
  257.     len = 1 + strlen(" >") + strlen(tempName);
  258.     for(i = 0 ; command[i] != NULL ; i++)
  259.         len += strlen(command[i]) + 1;
  260.  
  261.     cmd = (char *)malloc(len);
  262.     if(cmd != NULL)
  263.     {
  264.         BPTR in,out;
  265.  
  266.         for(i = 0 ; command[i] != NULL ; i++)
  267.         {
  268.             if(i == 0)
  269.             {
  270.                 strcpy(cmd,command[i]);
  271.             }
  272.             else
  273.             {
  274.                 strcat(cmd," ");
  275.                 strcat(cmd,command[i]);
  276.             }
  277.         }
  278.  
  279.         strcat(cmd," >");
  280.         strcat(cmd,tempName);
  281.  
  282.         in = Open("NIL:",MODE_OLDFILE);
  283.         out = Open("CONSOLE:",MODE_NEWFILE);
  284.  
  285.         if(in != NULL && out != NULL)
  286.         {
  287.             LONG rc;
  288.  
  289.             rc = SystemTags(cmd,
  290.                 SYS_UserShell,    TRUE,
  291.                 SYS_Input,    in,
  292.                 SYS_Output,    out,
  293.                 SYS_Asynch,    TRUE,
  294.             TAG_DONE);
  295.  
  296.             if(rc == -1)
  297.             {
  298.                 Close(in);
  299.                 Close(out);
  300.             }
  301.  
  302.             in = NULL;
  303.             out = NULL;
  304.  
  305.             if(rc != 0)
  306.             {
  307.                 fprintf(stderr,"cxref: Can not fork for the cpp command.\n");
  308.                 exit(1);
  309.             }
  310.  
  311.             result = fopen(tempName,"rb");
  312.             if(result == NULL)
  313.             {
  314.                 fprintf(stderr,"cxref: Can not pipe for the cpp command.\n");
  315.                 exit(1);
  316.             }
  317.         }
  318.         else
  319.         {
  320.             if(in != NULL)
  321.                 Close(in);
  322.  
  323.             if(out != NULL)
  324.                 Close(out);
  325.  
  326.             fprintf(stderr,"cxref: Can not pipe for the cpp command.\n");
  327.             exit(1);
  328.         }
  329.  
  330.         if(in != NULL)
  331.             Close(in);
  332.  
  333.         if(out != NULL)
  334.             Close(out);
  335.     }
  336.  
  337.     if(cmd != NULL)
  338.         free(cmd);
  339.  
  340.     return(result);
  341. }
  342.  
  343. int
  344. pclose_execvp(FILE* f)
  345. {
  346.     fclose(f);
  347.     return(0);
  348. }
  349.  
  350. /******************************************************************************/
  351.  
  352. FILE *
  353. amiga_fopen(char *name,const char *mode)
  354. {
  355.     if(!strncmp(name,"./",2) || !strncmp(name,"..",2))
  356.         name += 2;
  357.  
  358.     return(fopen(name,mode));
  359. }
  360.  
  361. int
  362. amiga_stat(char *name, struct stat *statstruct)
  363. {
  364.     if(!strncmp(name,"./",2) || !strncmp(name,"..",2))
  365.         name += 2;
  366.  
  367.     return(stat(name,statstruct));
  368. }
  369.  
  370. int
  371. amiga_mkdir(char *name)
  372. {
  373.     if(!strncmp(name,"./",2) || !strncmp(name,"..",2))
  374.         name += 2;
  375.  
  376.     return(mkdir(name));
  377. }
  378.  
  379. int
  380. amiga_lstat(char *name, struct stat *statstruct)
  381. {
  382.     if(!strncmp(name,"./",2) || !strncmp(name,"..",2))
  383.         name += 2;
  384.  
  385.     return(lstat(name,statstruct));
  386. }
  387.  
  388. int
  389. amiga_unlink(char *name)
  390. {
  391.     if(!strncmp(name,"./",2) || !strncmp(name,"..",2))
  392.         name += 2;
  393.  
  394.     return(unlink(name));
  395. }
  396.  
  397. int
  398. amiga_rename(char *oldname,char *newname)
  399. {
  400.     if(!strncmp(oldname,"./",2) || !strncmp(oldname,"..",2))
  401.         oldname += 2;
  402.  
  403.     if(!strncmp(newname,"./",2) || !strncmp(newname,"..",2))
  404.         newname += 2;
  405.  
  406.     return(rename(oldname,newname));
  407. }
  408.